<template>
  <div class="main-menu">
    <div class="logo">
      <img src="@/assets/img/logo.svg" alt="">
      <span v-show="!isFold">STONE-CMS</span>
    </div>
    <div class="menu">
      <el-menu
        :default-active="activeDefaults"
        background-color="#0c2135"
        text-color="#b7bdc3"
        active-text-color="#fff"
        :collapse="isFold"
      >
      <template v-for="item in userMenus" :key="item.id">
        <el-sub-menu  :index="item.id + ''">
          <template #title>
            <el-icon>
              <component :is="item.icon.split('-icon-')[1]" />
            </el-icon>
            <span>{{item.name}}</span>
          </template>
          <template v-for="subitem in item.children" :key="subitem.id">
            <el-menu-item :index="subitem.id + ''" @click="handleItemClick(subitem)">{{subitem.name}}</el-menu-item>
          </template>  
        </el-sub-menu>
      </template>
        

      </el-menu>
    </div>
  </div>
</template>

<script lang="ts" setup>
import useLoginStore from '@/store/login/login'
import { useRouter, useRoute } from 'vue-router'
import { computed } from 'vue'
import { mapPathMenu } from '@/utils/map-menus'

//定义props
defineProps({
  isFold: {
    type: Boolean,
    default: false
  }
})

const loginStore = useLoginStore()
const userMenus  = loginStore.userMenus


const router = useRouter()

const handleItemClick = (item: any) => {
  const url = item.url
  router.push(url)
}

//菜单活跃
const route = useRoute()

const activeDefaults = computed(()=> {
  const pathMenu = mapPathMenu(route.path, userMenus)
  return pathMenu.id + ''
})


</script>

<style lang="less" scoped>
.main-menu {
  height: 100%;
  background-color: #001529;
  .logo {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    img {
      width: 30px;
    }

  }
  .menu {
    .el-menu {
      border-right: none;
      .el-menu-item {
        padding-left: 50px !important;
        background-color: #0c2135;
      }
      .el-menu-item:hover {
        color: #fff;
      }
      .el-menu-item.is-active {
        background-color: #0a60bd;
      }
    }
  }
}
</style>